package com.wiim.ui;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.wiim.adapter.BannerAdapter;
import com.wiim.adapter.ListAdapter;
import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.common.Utils;
import com.wiim.handler.CMCmdHandler;
import com.wiim.handler.SHOPCmdHandler;
import com.wiim.interfaceapp.IServiceCommandResult;
import com.wiim.libs.GPSTracker;
import com.wiim.model.BannerModel;
import com.wiim.model.ShopData;
import com.wiim.model.UserData;
import com.wiim.nhapnhap.AppGlobal;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.CustomGoogleMapFragment;
import com.wiim.nhapnhap.EgoodApp;
import com.wiim.nhapnhap.R;
import com.wiim.ui.StoreDetailFragment.MyDialogFragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.DirectionalViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class HomeFrament extends CustomGoogleMapFragment implements  OnClickListener, IServiceCommandResult, OnFocusChangeListener, OnPageChangeListener, OnMapClickListener, OnItemClickListener {
	LoginActivity login = new LoginActivity();
	private Button btnProfile;
	private Button btnGilf;
	private RelativeLayout rlTop;
	private RelativeLayout rlMap;
	private Button btnSearch;
	private ImageView btnQrcode;
	private Boolean isShowList = false;
	private TextView tvSummoney;

	private DirectionalViewPager pagerAdBanner;
	private BannerAdapter adBannerAdapter;
	private ArrayList<BannerModel> list = new ArrayList<BannerModel>();
	ArrayList<Button> listButton = new ArrayList<Button>();
	private LinearLayout lnChoise;
	private Button btnBG;
	private LinearLayout mapviewLinearLayout;
	public static Fragment fragcontext;
	public static ArrayList<ShopData> ExList = new ArrayList<ShopData>();
	private EditText edSearch;
	private ArrayAdapter<String> adapter;
	private ListAdapter searchAdapter;
	private ListView lv;
	private Button btnMylocation;
	ArrayList<String> listSearch = new ArrayList<String>();
	private static String _transactiontype = ConstantValue.PointTransactionType.EXCHANGE_AWARD.toString();

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.mail, container, false);
		Utilities.getGlobalVariable(getActivity()).isMapSearch = true;
		fragcontext = HomeFrament.this;

		// LayoutParams lp=pagerAdBanner.getLayoutParams();
		// lp.width=(int)
		// (Utilities.getGlobalVariable(getActivity()).device_width/1.1);
		// lp.height=(int)
		// (Utilities.getGlobalVariable(getActivity()).device_height/2);
		// pagerAdBanner.setLayoutParams(lp);
		// String domain = "www..xyzw.com";
		// String[] results = domain.split("\\..");

		init(view);
		getData1();

		// initData();
		// initBanner();

		return view;
	}

	private void getData1() {
		Bundle b = new Bundle();
		b.putString(EgoodApp.REST_OBJECT_PUT, "*");
		AppGlobal.getInstance(HomeFrament.this).startServiceCmd(SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID, new SHOPCmdHandler(), b, true);

	}

	private void initData() {
		btnProfile.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
		btnGilf.setOnClickListener(this);
		btnQrcode.setOnClickListener(this);
		pagerAdBanner.setOnFocusChangeListener(this);
		btnMylocation.setOnClickListener(this);
		initDefaultPoint(Utilities.getGlobalVariable(getActivity()).latlongDefault, false);
//		setMakerMap("ssss", Utilities.getGlobalVariable(getActivity()).latlongDefault);

	}

	protected void setUpMapIfNeeded() {
		// showDialog(getActivity(),
		// getActivity().getString(R.string.str_installgoogle));
		if (mMap == null) {
			mMapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapMain));
			mMap = mMapFragment.getMap();
			if (mMap != null) {
				mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getCurrentLocation(), 11));
				mMap.setOnMapClickListener(this);
			} else {
//				mapviewLinearLayout.setVisibility(View.GONE);
				showDialog(getActivity(), getActivity().getString(R.string.str_installgoogle));
			}
		}
	}

	private void showDialog(final Context context, final String message) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setNeutralButton(getString(R.string.str_Chapnhan), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(final DialogInterface dialog, final int id) {

				Intent intentMarket = new Intent(Intent.ACTION_VIEW, Uri.parse(ConstantValue.LINK_GOOGLE_PLAY_SERVICE));
				startActivity(intentMarket);

			}
		});
		builder.setNegativeButton(getString(R.string.str_Huy), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				

			}

		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	private void initBanner() {
		BannerModel bannerModel;
		// for (int i = 0; i < ExList.size(); i++) {
		// bannerModel = new BannerModel(R.drawable.pic2,
		// ExList.get(i).getShopName());
		//
		// list.add(bannerModel);
		// }
		adBannerAdapter = new BannerAdapter(getActivity(), ExList);
		pagerAdBanner.setAdapter(adBannerAdapter);
		pagerAdBanner.setCurrentItem(0);
		pagerAdBanner.setOnPageChangeListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (Utilities.getGlobalVariable(getActivity()).device_width / 60),
				(int) (Utilities.getGlobalVariable(getActivity()).device_height / 98.46));
		params.weight = 1.0f;
		// LinearLayout lnTv;
		listButton = new ArrayList<Button>();
		for (int i = 0; i < ExList.size(); i++) {
			Button btnChoise = new Button(getActivity());
			if (i == 0) {
				btnChoise.setBackgroundResource(R.drawable.icon_choise);
			} else {
				btnChoise.setBackgroundResource(R.drawable.icon_not_choise);
			}
			params.leftMargin = 5;
			btnChoise.setLayoutParams(params);
			listButton.add(btnChoise);
			lnChoise.addView(btnChoise);
		}
		setAutoPager(0);
	}

	Handler handler = new Handler();

	public void setAutoPager(final int index) {
		handler.removeMessages(0);
		handler.postDelayed(new Runnable() {
			public void run() {
				if (index < ExList.size() - 1) {
					pagerAdBanner.setCurrentItem(index + 1);
				} else if (index == ExList.size() - 1) {
					pagerAdBanner.setCurrentItem(0);
				}
			}
		}, 3000);
	}

	private void init(View view) {
//		gps = new GPSTracker(getActivity());
		btnProfile = (Button) getActivity().findViewById(R.id.btnProfile);
		btnQrcode = (ImageView) view.findViewById(R.id.btnQr);
		btnGilf = (Button) getActivity().findViewById(R.id.btnGilf);
		rlTop = (RelativeLayout) getActivity().findViewById(R.id.rlTop);
		rlMap = (RelativeLayout) view.findViewById(R.id.rlMap);
		btnSearch = (Button) view.findViewById(R.id.btnSearch);
		pagerAdBanner = (DirectionalViewPager) view.findViewById(R.id.pagerAdBanner);
		lnChoise = (LinearLayout) view.findViewById(R.id.lnChoise);
		btnBG = (Button) view.findViewById(R.id.btnBG);
		// btnSearch.setText("Tim kiem cua hang");
		tvSummoney = (TextView) getActivity().findViewById(R.id.tvSummoney);
		edSearch = (EditText) view.findViewById(R.id.edSearch);
		lv = (ListView) view.findViewById(R.id.list_view);
		btnMylocation = (Button) view.findViewById(R.id.btnMylocation);
		lv.setOnItemClickListener(this);
		tvSummoney.setText( login.getUserModel().getRewardPoints());

		edSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
				// When user changed the Text
				if (TextUtils.isEmpty(edSearch.getText())) {
					lv.setVisibility(View.GONE);
				} else {
					lv.setVisibility(View.VISIBLE);
				}

				adapter.getFilter().filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// lv.setVisibility(View.GONE);
			}
		});
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		releaseMap();
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		rlTop.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		if (v == btnProfile) {
			Utilities.getGlobalVariable(getActivity()).isMap = false;
			((CustomFragmentActivity) getActivity()).replaceFragment(ConstantValue.THONGTIN_TK_FRAGMENT, false);
		}
		if (v == btnSearch) {
			if (isShowList) {
				isShowList = false;
				btnSearch.setText(getString(R.string.str_searchstore));
				// pagerAdBanner.setVisibility(View.VISIBLE);
				// lnChoise.setVisibility(View.VISIBLE);
				showLayout(R.animator.slide_out_to_bottom, View.GONE);

			} else {
				// lnChoise.setVisibility(View.GONE);
				// pagerAdBanner.setVisibility(View.GONE);
				btnSearch.setText(getString(R.string.str_backhome));

				showLayout(R.animator.slide_in_from_bottom, View.VISIBLE);
				isShowList = true;
				setMapOverlay(ExList, true);
			}
		}
		if (v == btnGilf) {
			// rlTop.setVisibility(View.GONE);
			Utilities.getGlobalVariable(getActivity()).isMap = false;
			((CustomFragmentActivity) getActivity()).replaceFragment(ConstantValue.DOITHUONG_FRAGMENT, false);
		}
		
		if (v == btnQrcode) {
			Utilities.getGlobalVariable(getActivity()).isMap = false;
			TurnWebcameOn();
		}
		if (v == btnMylocation) {
			initDefaultPoint( Utilities.getGlobalVariable(getActivity()).latlongDefault, true);
		}

	}
//	public static GPSTracker gps;
//	public LatLng initGetLocation() {
//		// check if GPS enabled
//		LatLng srcGeoPoint;
//		if (gps.canGetLocation()) {
//			double latitude = gps.getLatitude();
//			double longitude = gps.getLongitude();
//			srcGeoPoint = new LatLng(latitude, longitude);
//			mCurrentPoint = srcGeoPoint;
//			initDefaultPoint(srcGeoPoint, true);
//		} else {
//			gps.showSettingsAlert();
//			double latitude = 10.77306;
//			double longitude = 106.69833;
//			srcGeoPoint = new LatLng(latitude, longitude);
//			mCurrentPoint = srcGeoPoint;
//			initDefaultPoint(srcGeoPoint, true);
//		}
//		return srcGeoPoint;
//	}
	public static final int REQUEST_CODE_QR = 0x0000c0de;

	public void TurnWebcameOn() {
		Intent intent = new Intent("com.google.zxing.client.android.SCANQR");
		intent.setPackage("com.wiim.nhapnhap");
		intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		startActivityForResult(intent, REQUEST_CODE_QR);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == REQUEST_CODE_QR) {
			if (resultCode == Activity.RESULT_OK) {
				String tagCode = intent.getStringExtra("SCAN_RESULT").trim();
				Log.i("QRcode: ", tagCode);
				
				if ( tagCode.matches(".*[0-9].*") && tagCode.matches(".*[A-Z].*") ) {
						getData(tagCode, "NONE");
				}else{
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_qrtag_invalid), Toast.LENGTH_LONG).show();
				}
			}
		}
	}

	private void getData(String qrcode, String shopid) {
		Bundle cmdData = new Bundle();
		cmdData.putString("qrcode", qrcode);
		cmdData.putString("shopid", shopid);
		AppGlobal.getInstance(HomeFrament.this).startServiceCmd(SHOPCmdHandler.COMMAND_GET_SHOP_BY_QRCODE, new SHOPCmdHandler(), cmdData, true);
	}

	private static ShopData shopdata;

	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		Bundle bResultData = (Bundle) resultData;

		switch (commandCode) {
		case SHOPCmdHandler.COMMAND_GET_SHOP_BY_QRCODE: {
			shopdata = (ShopData) bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);
			if (!shopdata.getShopID().equals("")) {

				if (Boolean.valueOf(shopdata.ExchangeAwardModel.ExchangeAwardStatus) == true && Boolean.valueOf(shopdata.RewardModel.RewardStatus) == true)
					// showDialog();
					showDialogSelectSearch();
				else
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_promition_unavailable), Toast.LENGTH_LONG).show();

			} else {
				Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_nosuitable_store), Toast.LENGTH_LONG).show();
			}
			Log.i("COMMAND_GET_SHOP_BY_QRCODE", "Received");
		}
			break;

		case SHOPCmdHandler.COMMAND_DO_POINTSTRANSACTION: {
			String AddRewardPointsResult = (String) bResultData.getString(EgoodApp.REST_OBJECT_RETURN);
			AddRewardPointsResult = AddRewardPointsResult.replace("\"", "");

			String[] results = AddRewardPointsResult.split("\\..");

			String responsecode = results[0];
			String userbalance = results[1];

			Log.i("responsecode", responsecode);
			Log.i("userbalance", userbalance);

			if (responsecode.equalsIgnoreCase(ConstantValue.ValidateDoPointTransactionResponse.SUCCESS.toString())) {
				dialog.dismiss();

				if (_transactiontype.equals(ConstantValue.PointTransactionType.REWARD_POINTS.toString())) {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_add_rewardpoints_success), Toast.LENGTH_LONG).show();

				} else if (_transactiontype.equals(ConstantValue.PointTransactionType.EXCHANGE_AWARD.toString())) {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_exchangeaward_success), Toast.LENGTH_LONG).show();
				}

				new LoginActivity().getUserModel().RewardAmount = userbalance;

				tvSummoney.setText(new LoginActivity().getUserModel().getRewardPoints());

			} else {
				if (responsecode.equalsIgnoreCase(ConstantValue.ValidateDoPointTransactionResponse.NOT_AVAILABLE.toString())) {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_promition_unavailable), Toast.LENGTH_LONG).show();
				} else if (responsecode.equalsIgnoreCase(ConstantValue.ValidateDoPointTransactionResponse.NOT_ENOUGH_MONEY.toString())) {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_not_enough_money), Toast.LENGTH_LONG).show();
				} else if (responsecode.equalsIgnoreCase(ConstantValue.ValidateDoPointTransactionResponse.INVALID_VERTIFYCODE.toString())) {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_verifycode_wrong), Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(HomeFrament.this.getActivity(), getResources().getString(R.string.msg_add_rewardpoints_fail), Toast.LENGTH_LONG).show();
				}
			}

			Log.i("COMMAND_ADD_REWARDPOINTS", AddRewardPointsResult);

		}
			break;
		case SHOPCmdHandler.COMMAND_GET_SHOPLIST_BY_CATEGORYID: {
			ShopData[] exlst = (ShopData[]) bResultData.getSerializable(EgoodApp.REST_OBJECT_RETURN);
			bindData(exlst);
		}
			break;

		default:
			break;
		}
	}

	private void bindData(ShopData[] exModel) {
		ExList = new ArrayList<ShopData>(Arrays.asList(exModel));

		initBanner();
		for (int i = 0; i < ExList.size(); i++) {
			listSearch.add(ExList.get(i).getShopName());
		}
		adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, listSearch);
		lv.setAdapter(adapter);
	}

	public static DialogFragment newFragment = null;

	public  void doRewardPoints(String transactiontype, String verifycode) {

		_transactiontype = transactiontype;

		Bundle cmdData = new Bundle();

		cmdData.putString("transactiontype", transactiontype);

		cmdData.putString("userid", new LoginActivity().getUserModel().UserId);
		cmdData.putString("fullname", new LoginActivity().getUserModel().Fullname);

		cmdData.putString("shopid", shopdata.ShopID);
		cmdData.putString("shopname", shopdata.ShopName);

		cmdData.putString("rewardid", shopdata.RewardModel.RewardID);
		cmdData.putString("exchangeawardid", shopdata.ExchangeAwardModel.ExchangeAwardID);

		cmdData.putString("verifycode", verifycode);

		AppGlobal.getInstance(fragcontext).startServiceCmd(SHOPCmdHandler.COMMAND_DO_POINTSTRANSACTION, new SHOPCmdHandler(), cmdData, true);
	}

//	public void showDialog() {
//		// if (!ScanningByQrcode)
//		// return;
//		// DialogFragment.show() will take care of adding the fragment
//		// in a transaction. We also want to remove any currently showing
//		// dialog, so make our own transaction and take care of that here.
//		FragmentTransaction ft = getFragmentManager().beginTransaction();
//		Fragment prev = getFragmentManager().findFragmentByTag("dialog");
//		if (prev != null) {
//			ft.remove(prev);
//		}
//		ft.addToBackStack(null);
//		// Create and show the dialog.
//		newFragment = MyDialogFragment.newInstance(1);
//		newFragment.show(ft, "dialog");
//
//		// newFragment.dismiss();
//	}

//	public static class MyDialogFragment extends DialogFragment implements OnClickListener {
//		int mNum;
//
//		static MyDialogFragment newInstance(int num) {
//			MyDialogFragment f = new MyDialogFragment();
//
//			// Supply NUM input as an argument.
//			Bundle args = new Bundle();
//			args.putInt("num", num);
//			f.setArguments(args);
//
//			return f;
//		}
//
//		@Override
//		public void onDestroy() {
//			// TODO Auto-generated method stub
//			super.onDestroy();
//			lnDoithuong.setVisibility(View.VISIBLE);
//			lnInputVerifycode.setVisibility(View.GONE);
//		}
//
//		@Override
//		public void onCreate(Bundle savedInstanceState) {
//			super.onCreate(savedInstanceState);
//			mNum = getArguments().getInt("num");
//			setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar);
//		}
//
//		private Button btnDoithuong;
//		private Button btnTichdiem;
//		private LinearLayout lnDoithuong;
//		private LinearLayout lnInputVerifycode;
//		private String TransactionType = ConstantValue.PointTransactionType.REWARD_POINTS.toString();
//
//		@Override
//		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//			View v = inflater.inflate(R.layout.popup_exchange_options, container, false);
//
//			lnDoithuong = (LinearLayout) v.findViewById(R.id.lnDoithuong);
//			lnInputVerifycode = (LinearLayout) v.findViewById(R.id.lnInputVerifycode);
//
//			final EditText edtVerifycode = (EditText) v.findViewById(R.id.edtVerifycode);
//			final Button btnConfirmVerifycode = (Button) v.findViewById(R.id.btnConfirmVerifycode);
//
//			btnDoithuong = (Button) v.findViewById(R.id.btnDoithuong);
//			btnTichdiem = (Button) v.findViewById(R.id.btnTichdiem);
//
//			final TextView tvpShopName = (TextView) v.findViewById(R.id.tvpShopName);
//			final TextView tvpShopAddress = (TextView) v.findViewById(R.id.tvpShopAddress);
//
//			final ImageView imgStore = (ImageView) v.findViewById(R.id.imgStore);
//
//			// lnDoithuong.setVisibility(View.VISIBLE);
//			// lnInputVerifycode.setVisibility(View.GONE);
//
//			tvpShopName.setText(shopdata.ShopName);
//			tvpShopAddress.setText(shopdata.Address);
//			if (shopdata.getAvatar() != null) {
//				Bitmap bitmap = BitmapFactory.decodeByteArray(shopdata.getAvatar(), 0, shopdata.getAvatar().length);
//				imgStore.setImageBitmap(bitmap);
//			} else {
//				imgStore.setImageDrawable(getResources().getDrawable(R.drawable.no_image));
//			}
//
//			Bitmap bitmap = BitmapFactory.decodeByteArray(shopdata.getAvatar(), 0, shopdata.getAvatar().length);
//			imgStore.setImageBitmap(bitmap);
//
//			btnDoithuong.setOnClickListener(this);
//			btnTichdiem.setOnClickListener(this);
//			// btnDoithuong.setOnClickListener(new OnClickListener() {
//			// @Override
//			// public void onClick(View v) {
//			// lnDoithuong.setVisibility(View.GONE);
//			// lnInputVerifycode.setVisibility(View.VISIBLE);
//			// TransactionType =
//			// ConstantValue.PointTransactionType.EXCHANGE_AWARD.toString();
//			//
//			// }
//			// });
//
//			// btnTichdiem.setOnClickListener(new OnClickListener() {
//			// @Override
//			// public void onClick(View v) {
//			// lnDoithuong.setVisibility(View.GONE);
//			// lnInputVerifycode.setVisibility(View.VISIBLE);
//			// TransactionType =
//			// ConstantValue.PointTransactionType.REWARD_POINTS.toString();
//			//
//			// }
//			// });
//
//			btnConfirmVerifycode.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					if (TextUtils.isEmpty(edtVerifycode.getText())) {
//						Toast.makeText(getActivity(), getString(R.string.str_Vuilong), Toast.LENGTH_SHORT).show();
//					} else {
//						doRewardPoints(TransactionType, edtVerifycode.getText().toString().trim());
//					}
//
//				}
//			});
//
//			return v;
//		}

//		@Override
//		public void onClick(View v) {
//			if (v == btnDoithuong) {
//				lnDoithuong.setVisibility(View.GONE);
//				lnInputVerifycode.setVisibility(View.VISIBLE);
//				TransactionType = ConstantValue.PointTransactionType.EXCHANGE_AWARD.toString();
//			}
//			if (v == btnTichdiem) {
//				lnDoithuong.setVisibility(View.GONE);
//				lnInputVerifycode.setVisibility(View.VISIBLE);
//				TransactionType = ConstantValue.PointTransactionType.REWARD_POINTS.toString();
//			}
//		}
//	}

	public void showLayout(int anim, int view) {
		Animation animation = AnimationUtils.loadAnimation(getActivity(), anim);
		animation.setDuration(2500);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				btnBG.setVisibility(View.VISIBLE);
				lnChoise.setVisibility(View.GONE);
				pagerAdBanner.setVisibility(View.GONE);

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				btnBG.setVisibility(View.GONE);
				pagerAdBanner.setVisibility(View.VISIBLE);
				lnChoise.setVisibility(View.VISIBLE);
				Utilities.getGlobalVariable(getActivity()).isMap = true;

			}
		});
		rlMap.startAnimation(animation);
		rlMap.setVisibility(view);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		rlTop.setVisibility(View.VISIBLE);
		setUpMapIfNeeded();
		if (mMap != null) {
			initData();
		}
		// if (Utilities.getGlobalVariable(getActivity()).isMap) {
		// btnSearch.setText(getString(R.string.str_backhome));
		// setMapOverlay(ExList, true);
		// rlMap.setVisibility(View.VISIBLE);
		// btnBG.setVisibility(View.GONE);
		// lnChoise.setVisibility(View.VISIBLE);
		// isShowList = true;
		// }
//		 Log.v("HAI", "userid : " + login.getUserModel().getUserId());
//		 Log.v("HAI", "username : " + login.getUserModel().getUsername());
//		 Log.v("HAI", "Curent points : " + login.getUserModel().getRewardPoints());
//		 Log.v("HAI", "diviceID : " + login.getUserModel().getDeviceID());
//		 Log.v("HAI", "pass : " + login.getUserModel().getPassword());
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		for (int i = 0; i < ExList.size(); i++) {
			if (arg0 == i) {
				listButton.get(i).setBackgroundResource(R.drawable.icon_choise);
			} else {
				listButton.get(i).setBackgroundResource(R.drawable.icon_not_choise);
			}
		}
		setAutoPager(arg0);

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMapClick(LatLng arg0) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unused")
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0 == lv) {
			String str = lv.getItemAtPosition(arg2).toString();
			((CustomFragmentActivity) getActivity()).setPassValue("SHOPNAME", str);
			((CustomFragmentActivity) getActivity()).replaceFragment(ConstantValue.STORE_DETAIL_FRAGMENT, false);
		}
	}

	/**
	 * function show dialog login
	 */
	public Dialog dialog;
	private Button btnDoithuong;
	private Button btnTichdiem;
	private String TransactionType = ConstantValue.PointTransactionType.REWARD_POINTS.toString();
	public Boolean isShowDialog = false;

	public void showDialogSelectSearch() {
		// custom dialog
		if (!isShowDialog) {
			isShowDialog = true;
			dialog = new Dialog(getActivity());
			dialog.getWindow();
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.search_dialog_select);
			dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
			// dialog.setTitle("Title...");

			// set the custom dialog components - text, image and button

			final EditText edtVerifycode = (EditText) dialog.findViewById(R.id.edDoithuong);
			final Button btnConfirmVerifycode = (Button) dialog.findViewById(R.id.btnConfirmVerifycode);

			btnDoithuong = (Button) dialog.findViewById(R.id.btnDoithuong);
			btnTichdiem = (Button) dialog.findViewById(R.id.btnTichdiem);

			final TextView tvpShopName = (TextView) dialog.findViewById(R.id.tvpShopName);
			final TextView tvpShopAddress = (TextView) dialog.findViewById(R.id.tvpShopAddress);

			final ImageView imgStore = (ImageView) dialog.findViewById(R.id.imgStore);

			tvpShopName.setText(shopdata.ShopName);
			tvpShopAddress.setText(shopdata.Address);
			if (shopdata.getAvatar() != null) {
//				Bitmap bitmap = BitmapFactory.decodeByteArray(shopdata.getAvatar(), 0, shopdata.getAvatar().length);
//				imgStore.setImageBitmap(bitmap);
				
				new DownloadImageTask(imgStore).execute(shopdata.getAvatar());
				
			} else {
				imgStore.setImageDrawable(getResources().getDrawable(R.drawable.no_image));
			}

//			Bitmap bitmap = BitmapFactory.decodeByteArray(shopdata.getAvatar(), 0, shopdata.getAvatar().length);
//			imgStore.setImageBitmap(bitmap);

			// btnDoithuong.setOnClickListener(this);
			// btnTichdiem.setOnClickListener(this);
			btnDoithuong.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					btnDoithuong.setVisibility(View.GONE);
					btnTichdiem.setVisibility(View.GONE);
					edtVerifycode.setVisibility(View.VISIBLE);
					btnConfirmVerifycode.setVisibility(View.VISIBLE);
					TransactionType = ConstantValue.PointTransactionType.EXCHANGE_AWARD.toString();

				}
			});

			btnTichdiem.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					btnDoithuong.setVisibility(View.GONE);
					btnTichdiem.setVisibility(View.GONE);
					edtVerifycode.setVisibility(View.VISIBLE);
					btnConfirmVerifycode.setVisibility(View.VISIBLE);
					TransactionType = ConstantValue.PointTransactionType.REWARD_POINTS.toString();

				}
			});

			btnConfirmVerifycode.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (TextUtils.isEmpty(edtVerifycode.getText())) {
						Toast.makeText(getActivity(), getString(R.string.str_Vuilong), Toast.LENGTH_SHORT).show();
					} else {
						doRewardPoints(TransactionType, edtVerifycode.getText().toString().trim());
					}

				}
			});

			dialog.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					isShowDialog = false;
				}
			});

			dialog.show();
		}

	}
	@Override
	public void onInfoWindowClick(Marker arg0) {
		// TODO Auto-generated method stub
		for (int i = 0; i < listMarker.size(); i++) {
			if (arg0.equals(listMarker.get(i))) {
				startLocationDetail(ExList.get(i).ShopID, arg0.getPosition());
				break;
			}
		}
		super.onInfoWindowClick(arg0);
	}
	@Override
	public void startLocationDetail(final String position, final LatLng point) {
		// TODO Auto-generated method stub
		super.startLocationDetail(position, point);
		CustomFragmentActivity activity = (CustomFragmentActivity) this.getActivity();
		activity.setPassValue("SHOPID", position);
//		activity.setPassValue(ConstantValue.BUSINESS_POINT, point);
		activity.replaceFragment(ConstantValue.STORE_DETAIL_FRAGMENT, false);
	}
	
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
		    this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
		    String urldisplay = urls[0];
		    Bitmap mIcon11 = null;
		    try {
		        InputStream in = new java.net.URL(urldisplay).openStream();
		        mIcon11 = BitmapFactory.decodeStream(in);
		    } catch (Exception e) {
		        Log.e("Error", e.getMessage());
		        e.printStackTrace();
		    }
		    return mIcon11;
		}

		protected void onPostExecute(Bitmap result) {		   
			bmImage.setImageBitmap(result);
		}
	}

}
